
package com.freework.util;

import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
public class SpringBeanUtil
   
{

	private  ApplicationContext applicationContext;
	private SpringBeanUtil(){}

	private static SpringBeanUtil myobj=new SpringBeanUtil();
	
	public static SpringBeanUtil bean(){
		return myobj;
	}
	
    public void setApplicationContext(ApplicationContext context)
        throws BeansException
    {
        applicationContext = context;
    }

    public  Object getBeanByClass(Class clazz)
    {
        Map map = applicationContext.getBeansOfType(clazz);
        for(Iterator iter = map.keySet().iterator(); iter.hasNext();)
        {
            Object bean = map.get(iter.next());
            if(bean != null)
                return bean;
        }

        return null;
    }

    public  Object getBeanByName(String name)
    {
    	if(applicationContext==null)
    		return null;
        return applicationContext.getBean(name);
    }
    public static Object getBean(String name)
    {
        return (myobj==null?null:myobj.getBeanByName(name));
    }
    public  ApplicationContext getApplicationContext()
    {
        return applicationContext;
    }

    public  Method getMethod(Class clazz, String methodName)
    {
        return BeanUtils.findMethodWithMinimalParameters(clazz, methodName);
    }


   
}